package com.ymatou.iapi.marketingmanage.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ymatou.iapi.marketingmanage.parameter.CreateSellerPromotionBean;
import com.ymatou.iapi.marketingmanage.parameter.Promotion;
import com.ymatou.iapi.marketingmanage.parameter.PromotionPolicy;
import com.ymatou.iapi.marketingmanage.parameter.PromotionValue;
import com.ymatou.iapi.marketingmanage.parameter.SellerPromotionListByStateBean;
import com.ymatou.iapi.marketingmanage.service.GetPromotionByIdCall;
import com.ymatou.iapi.marketingmanage.service.SellerPromotionListByStateCall;
import com.ymatou.iapi.trading.operate.parameter.enums.PromotionTypeEnum;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JSellerPromotionCallService;
import com.ymttest.database.mongo.YmtBuyerProductWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*根据状态查询买手促销活动列表
*FreeMaker Template自动生成代码
*mark:实际是当前有效活动
*/
@TestSuite
public class Ts_SellerPromotionListByState{
	private static SellerPromotionListByStateBean sellerpromotionlistbystateBean;
	private static SellerPromotionListByStateCall sellerpromotionlistbystateCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据状态查询买手促销活动列表(查询当前有效活动)");
	}

	@Before
	public void caseUp() {
		sellerpromotionlistbystateBean=new SellerPromotionListByStateBean();
		sellerpromotionlistbystateCall=new SellerPromotionListByStateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellerpromotionlistbystateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyData(SellerPromotionListByStateCall sellerpromotionlistbystateCall2, Map<Integer,CreateSellerPromotionBean> mcspb,Integer sellerid) {
		JsonObject json = sellerpromotionlistbystateCall2.getJsonObject().getAsJsonObject("Data");
		JsonArray ja = json.get("Promotions").getAsJsonArray();
		for (JsonElement jsonElement : ja) {
			JsonObject recjson =jsonElement.getAsJsonObject();
			
			Promotion recp=(Promotion) FormatUtil.GSONToObject(recjson.toString(), Promotion.class);
			CreateSellerPromotionBean cspb = mcspb.get(recp.getPromotionId());
			PromotionPolicy sendp = cspb.getPromotionInfo();
			Logger.verifyEquals(sellerid,recp.getSellerId(),"验证sellerid");
			Logger.verifyEquals(recp.getPromotionName(),sendp.getPromotionName(),"验证PromotionName");
			Logger.verifyEquals(recp.getPromotionDesc(),sendp.getPromotionDesc(),"验证PromotionDesc");
			Logger.verifyEquals(recp.getPromotionBeginTime().substring(0,12),sendp.getPromotionBeginTime().substring(0,12),"验证PromotionBeginTime");
			Logger.verifyEquals(recp.getPromotionEndTime().substring(0,12),sendp.getPromotionEndTime().substring(0,12),"验证PromotionEndTime");
			Logger.verifyEquals(recp.getPromotionType(),sendp.getPromotionType(),"验证PromotionType");
			Logger.verifyEquals(recp.getIsSameProductUse(),sendp.getIsSameProductUse(),"验证IsSameProductUse");
			Logger.verifyEquals(recp.getIsLimitProduct(),sendp.getIsLimitProduct(),"验证IsLimitProduct");
			Logger.verifyEquals(recp.getIsSpecifyProduct(),sendp.getIsSpecifyProduct(),"验证IsSpecifyProduct");
			Logger.verifyEquals(recp.getCanUseSellerCoupon(),sendp.getCanUseSellerCoupon(),"验证CanUseSellerCoupon");
			
			PromotionValue spv = sendp.getPromotionValues().get(0);
			PromotionValue rpv = recp.getPromotionValues().get(0);
			Integer type = sendp.getPromotionType();
			if (type == 1) {
				Logger.verifyEquals(rpv.getQtyCondition(), spv.getQtyCondition(), "验证rv");
				Logger.verifyEquals(rpv.getValue(),spv.getValue(), "验证dv");

			}
			// 折扣的时候/10
			else if (type == 2) {
				Logger.verifyEquals(rpv.getQtyCondition(), spv.getQtyCondition(), "验证rv");
				Logger.verifyEquals(rpv.getValue(), spv.getValue().divide(BigDecimal.valueOf(10)) ,"验证dv");

			} else if (type == 3) {
				Logger.verifyEquals(rpv.getAmountCondition(), spv.getAmountCondition(), "验证rv");
				Logger.verifyEquals(rpv.getValue(), spv.getValue(), "验证dv");
			} else if (type == 4) {
				Logger.verifyEquals(rpv.getAmountCondition(), spv.getAmountCondition() ,"验证rv");
				Logger.verifyEquals(rpv.getValue(), spv.getValue().divide(BigDecimal.valueOf(10)), "验证dv");

			} else if (type == 5) {
				Logger.verifyEquals(2, spv.getQtyCondition(), "验证rv");
				Logger.verifyEquals(new BigDecimal(0.5), spv.getValue(), "验证dv");

			}
		}
		
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerPromotionListByState_001() {
		Logger.start(true,"根据状态查询买手促销活动列表成功IsShowPromotionDetail为true");
		try {
			Map<Integer,CreateSellerPromotionBean> mcspb=new HashMap<Integer, CreateSellerPromotionBean>();
			int sellerid=105;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			cspb.getPromotionInfo().getProductIds().add(bpw.get(0).get("ProductId").toString());
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			mcspb.put(pid, cspb);
			
			sellerpromotionlistbystateBean.setIsShowPromotionDetail(true);
			sellerpromotionlistbystateBean.setSellerId(sellerid);
			sellerpromotionlistbystateCall.setData(sellerpromotionlistbystateBean);
			sellerpromotionlistbystateCall.callService();			
			Logger.verifyEquals("操作成功", sellerpromotionlistbystateCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", sellerpromotionlistbystateCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", sellerpromotionlistbystateCall.getString("BCode"), "验证BCode");
			verifyData(sellerpromotionlistbystateCall, mcspb,sellerid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SellerPromotionListByState_002() {
		Logger.start(true,"不输入sellerid，失败");
		try {
			Map<Integer,CreateSellerPromotionBean> mcspb=new HashMap<Integer, CreateSellerPromotionBean>();
			int sellerid=105;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			cspb.getPromotionInfo().getProductIds().add(bpw.get(0).get("ProductId").toString());
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			mcspb.put(pid, cspb);
			
			sellerpromotionlistbystateBean.setIsShowPromotionDetail(true);
//			sellerpromotionlistbystateBean.setSellerId(sellerid);
			sellerpromotionlistbystateCall.setData(sellerpromotionlistbystateBean);
			sellerpromotionlistbystateCall.callService();	
			Logger.verifyIsContains("无效的买手Id", sellerpromotionlistbystateCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("400", sellerpromotionlistbystateCall.getString("Code"), "验证Code");
			Logger.verifyEquals("-1", sellerpromotionlistbystateCall.getString("BCode"), "验证BCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SellerPromotionListByState_003() {
		Logger.start(true,"不输入IsShowPromotionDetail，默认false");
		try {
			Map<Integer,CreateSellerPromotionBean> mcspb=new HashMap<Integer, CreateSellerPromotionBean>();
			int sellerid=105;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			cspb.getPromotionInfo().getProductIds().add(bpw.get(0).get("ProductId").toString());
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			mcspb.put(pid, cspb);
			
//			sellerpromotionlistbystateBean.setIsShowPromotionDetail(true);
			sellerpromotionlistbystateBean.setSellerId(sellerid);
			sellerpromotionlistbystateCall.setData(sellerpromotionlistbystateBean);
			sellerpromotionlistbystateCall.callService();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SellerPromotionListByState_004() {
		Logger.start(true,"根据状态查询买手促销活动列表成功IsShowPromotionDetail为false");
		try {
			Map<Integer,CreateSellerPromotionBean> mcspb=new HashMap<Integer, CreateSellerPromotionBean>();
			int sellerid=105;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(4);
			cspb.getPromotionInfo().getProductIds().add(bpw.get(0).get("ProductId").toString());
			int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
			mcspb.put(pid, cspb);
			
			sellerpromotionlistbystateBean.setIsShowPromotionDetail(false);
			sellerpromotionlistbystateBean.setSellerId(sellerid);
			sellerpromotionlistbystateCall.setData(sellerpromotionlistbystateBean);
			sellerpromotionlistbystateCall.callService();			
			Logger.verifyEquals("操作成功", sellerpromotionlistbystateCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", sellerpromotionlistbystateCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", sellerpromotionlistbystateCall.getString("BCode"), "验证BCode");
			JsonObject json = sellerpromotionlistbystateCall.getJsonObject().getAsJsonObject("Data");
			Logger.verifyEquals(1, json.get("TotalCount").getAsInt(), "验证TotalCount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerPromotionListByState_005() {
		Logger.start(true,"根据状态查询买手促销活动列表成功多个活动");
		try {
			Map<Integer,CreateSellerPromotionBean> mcspb=new HashMap<Integer, CreateSellerPromotionBean>();
			int sellerid=105;
			JSellerPromotionCallService.CancelEnablePromotion(sellerid);
			CreateSellerPromotionBean cspb = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.DiscountIfAmountEnough);
			cspb.getPromotionInfo().setIsSpecifyProduct(true);
			List<Map> bpw = new YmtBuyerProductWapper().getLimit(3);
			int i=1;
			for (Map map : bpw) {
				cspb.getPromotionInfo().getProductIds().clear();
				cspb.getPromotionInfo().getProductIds().add(map.get("ProductId").toString());
				int pid=JSellerPromotionCallService.CreateSellerPromotion(cspb);
				mcspb.put(pid, cspb);
				i++;
			}
			
			sellerpromotionlistbystateBean.setIsShowPromotionDetail(true);
			sellerpromotionlistbystateBean.setSellerId(sellerid);
			sellerpromotionlistbystateCall.setData(sellerpromotionlistbystateBean);
			sellerpromotionlistbystateCall.callService();			
			Logger.verifyEquals("操作成功", sellerpromotionlistbystateCall.getString("Msg"), "验证Msg");
			Logger.verifyEquals("200", sellerpromotionlistbystateCall.getString("Code"), "验证Code");
			Logger.verifyEquals("0", sellerpromotionlistbystateCall.getString("BCode"), "验证BCode");
			JsonObject json = sellerpromotionlistbystateCall.getJsonObject().getAsJsonObject("Data");
			Logger.verifyEquals(3, json.get("TotalCount").getAsInt(), "验证TotalCount");
			verifyData(sellerpromotionlistbystateCall, mcspb,sellerid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}